import router from './router/'
import store from './store/'
import { Notify } from 'vant'

// 公众号登录
// router.beforeEach((to, from, next) => {
//   if (to.matched.some(e => e.meta.needLogin === true)) {
//     if (store.getters.isLogin) {
//       next()
//     } else {
//       store.dispatch('doRedirectHandle', to.name)
//     }
//   } else {
//     next()
//   }
// })
// 账户密码登录
router.beforeEach((to, from, next) => {
  if (to.matched.some(e => e.meta.needLogin === true)) {
    if (store.getters.isLogin) {
      if (store.getters.hasUserinfo) {
        next()
      } else {
        store.dispatch('getUserinfo').then(() => {
          next()
        }).catch(err => {
          Notify({ type: 'warning', message: err.repsonse.data })
          next({ name: 'Login' })
        })
      }
    } else {
      Notify({ type: 'warning', message: '请登录！' })
      next({ name: 'Login' })
    }
  } else {
    next()
  }
})

router.afterEach(() => {
  // todo
})
